package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.CustomerDao;
import com.atguigu.jxc.dao.SupplierDao;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.Customer;
import com.atguigu.jxc.entity.Supplier;
import com.atguigu.jxc.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CustomerServiceImpl implements CustomerService {
    @Autowired
    private CustomerDao customerDao;
    @Override
    public Map<String, Object> list(Integer page, Integer rows, String customerName) {
        // 计算limit 的索引值 limit 0,30
        Integer index = (page - 1) * rows;
        // 查询goods的列表
        List<Customer> supplierList = customerDao.selectPage(index, rows, customerName);
        // 查询goods的数量
        Integer count = customerDao.selectCount(customerName);
        // 组装map
        Map<String, Object> map = new HashMap<>();

        map.put("total", count);
        map.put("rows", supplierList);
        return map;
    }


    @Transactional(rollbackFor = Exception.class)
    @Override
    public ServiceVO save(Integer customerId, Customer customer) {
        if (customerId == null) {
            // 添加
            customerDao.insert(customer);
        } else {
            // 修改
            customerDao.update(customer);
        }
        return new ServiceVO(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS, null);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public ServiceVO delete(String ids) {
        String[] idsArr = ids.split(",");
        customerDao.delete(idsArr);

        return new ServiceVO(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS, null);
    }
}
